import axios from 'axios'

function getToken () {
  let url = window.location.href
  let ret = url.match(/.*token=(.*)#\//)
  return ret != null ? ret[1] : ''
}
const service = axios.create({
  timeout: 5000
})

service.interceptors.request.use(
  config => {
    config.url +=
      (config.url.indexOf('?') < 0 ? '?' : '&') + '_t=' + new Date().getTime()

    config.headers['NBSSM_TOKEN'] = getToken()
    return config
  },
  error => {
    console.log(error)
    return Promise.reject(error)
  }
)

/**
 * 相应拦截根据项目情况而定
 */
service.interceptors.response.use(
  response => response,
  error => Promise.reject(error)
)

export default service
